package com.ft.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ft.domain.Buyorder;
import com.ft.service.IBuyorderService;
import com.ft.utils.Code;
import com.ft.utils.Result;
import com.ft.utils.SnowFlakeGenerateIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com/ft
 * @since 2022-08-24
 */
@RestController
@RequestMapping("/buyorder")
@CrossOrigin
public class BuyorderController {
    @Autowired
    private IBuyorderService buyorderService;

    /**
     * 创建订单
     * @param buyorder 订单对象
     * @return 创建的订单对象
     * @throws JsonProcessingException
     */
    @PostMapping()
    public Result creatrBuyOrder(@RequestBody Buyorder buyorder) throws JsonProcessingException {
        //创建订单编号
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker =
                new SnowFlakeGenerateIdWorker(0L,0L);
        String id = snowFlakeGenerateIdWorker.generateNextId();
        //设置订单编号
        buyorder.setId(Long.valueOf(id));
        //返回创建结果
        int result = buyorderService.createBuyOrder(buyorder);
        if (result == 1){
            Buyorder buyorder1 = buyorderService.selectBuyOrder(Long.valueOf(id));
            return new Result(Code.POST_OK,buyorder1,"创建订单成功");
        }else {
            return new Result(Code.POST_ERR,"创建订单失败");
        }
    }
}

